<?php

namespace app\admin\controller;

use app\admin\model\Node as NodeModel;
use think\Session;
use think\Db;

class IndexController extends BaseController
{
    //首页显示的信息
    public function index()
    {
        $staff = Session::get('staff');
        $this->assign([
//            'nodeList' => $this->makeNodeList(),
            'staff' => $staff,
        ]);
        return $this->fetch();
    }

    //处理nodeList 的数据生成后台导航列表
    private function makeNodeList()
    {
        $nodeList = Session::get('nodeList');
        unset($nodeList['index']);
        unset($nodeList['login']);
        foreach($nodeList as $k=>$v){
            foreach($v as $key=>$val){
                if($val !== "lst"){
                    unset($nodeList[$k][$key]);
                }else{
                    unset($nodeList[$k][$key]);
                    $nodeList[$k]['action'] = 'lst';
                    $nodeList[$k]['controller'] = $k;
                }
            }
        }
        $newNodeList = [];
        foreach($nodeList as $a=>$b){
            $res = Db::query(
                "SELECT `node`.name,`node`.controller,`node`.action, `admin_nav_group`.name as aname FROM `node` LEFT JOIN `admin_nav_group` ON `node`.ang_id = `admin_nav_group`.id WHERE node.action='".$b['action']."' AND node.controller='".$b['controller']."' AND node.status=1 AND node.delete_time is null");
            $b['name'] = $res[0]['name'];
            $newNodeList[$res[0]['aname']][] = $b;
            
            //dump($res);
            // $res = NodeModel::with('adminNavGroup')->where($b)->find();
            // $b['name'] = $res->name;
            // dump($res->admin_nav_group);
            // dump($res->admin_nav_group->name);
            
            // $newNodeList[$res->admin_nav_group->name][]  = $b;
        }
        //dump($newNodeList);
        return $newNodeList;
    }

    public function adminInfo()
    {
        // $staff = Session::get('staff');
        // $this->assign('staff',$staff);
        return $this->fetch('index/home');
    }


}
